package ustc.xiaoyang.karl.doublepointer;

/**
 * 844.比较含退格的字符串 easy
 */
public class CompareStringsWithBackspace844 {
    public static void main(String[] args) {

        System.out.println(new CompareStringsWithBackspace844().compare("ab#c","ad#c"));
    }
    public boolean compare(String str1,String str2)
    {
        String s1 = backspace(str1);
        String s2 = backspace(str2);
        return s1.equals(s2);
    }
    public String backspace(String str)
    {
        StringBuilder sb = new StringBuilder();
        int slow = 0;
        for (int fast = 0; fast < str.length(); fast++){
            if (str.charAt(fast) != '#'){
                sb.append(str.charAt(fast));
                slow++;
            } else if (slow > 0) {
                sb.deleteCharAt(slow - 1);
                slow--;
            }
        }
        return sb.toString();
    }
}
